/**
 * 模拟 API
 */
import {
	http
} from '../request/index.js' // 请求拦截
import Cache from '../../common/cache.js'

/**
 * 数据列表
 * 
 * @returns {Promise}
 */
export function getList() {
	return new Promise(resolve => {
		const cacheKey = 'data:list';
		const data = Cache.get(cacheKey);
		if (data) {
			resolve([0, data]);
		} else {
			setTimeout(() => {
				resolve([0, [{
					id: 1,
					type: 1
				}, {
					id: 2,
					type: 2
				}, {
					id: 3,
					type: 1
				}, {
					id: 4,
					type: 2
				}]]);
			}, 500);
			// http.get('/v1/data/list').then(([err, res]) => {
			// 	if (! err) {
			// 		Cache.put(cacheKey, res, 60);
			// 		resolve([0, res]);
			// 	} else {
			// 		resolve([err]);
			// 	}	
			// });
		}
	});
}

/**
 * 数据详情
 * 
 * @returns {Promise}
 */
export function getDetail(id, refresh) {
	return new Promise(resolve => {
		const cacheKey = 'data:detail:' + id;
		const data = Cache.get(cacheKey);
		if (data && !refresh) {
			resolve([0, data]);
		} else {
			setTimeout(() => {
				resolve([0, {
					id: 1,
					type: 1,
					name: '测试内容'
				}]);
			}, 500);
			// http.get('/v1/data/detail').then(([err, res]) => {
			// 	if (! err) {
			// 		Cache.put(cacheKey, res, 60);
			// 		resolve([0, res]);
			// 	} else {
			// 		resolve([err]);
			// 	}	
			// });
		}
	});
}
